package com.kamistoat.ruoyi.ruoyimallproduct.controller;

import com.kamistoat.ruoyi.ruoyicommoncore.utils.page.PageUtils;
import com.kamistoat.ruoyi.ruoyicommoncore.web.domain.AjaxResult;
import com.kamistoat.ruoyi.ruoyicommonsecurity.annotation.RequiresPermissions;
import com.kamistoat.ruoyi.ruoyimallproduct.entity.BrandEntity;
import com.kamistoat.ruoyi.ruoyimallproduct.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("mall:brand:list")
    public AjaxResult list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);
        return AjaxResult.success().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    @RequiresPermissions("mall:brand:info")
    public AjaxResult info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);
        return AjaxResult.success().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("mall:brand:save")
    // 使用BindingResult接收数据校验的错误异常，而如果删除掉BindingResult，异常被直接抛出，可以用@ControllerAdvice来接收。
    public AjaxResult save(@RequestBody BrandEntity brand) {
        brandService.save(brand);
        return AjaxResult.success();
    }

    /**
     * 修改原始的update方法，不再直接进行更新
     * 而是要连着具有约束关系的表一起更新
     */
    @RequestMapping("/update")
    @RequiresPermissions("mall:brand:update")
    public AjaxResult update(@RequestBody BrandEntity brand) {
        // TODO 冗余更新
//        brandService.updateDetail(brand);
        return AjaxResult.success();
    }

    /**
     * 仅仅修改状态
     */
    @RequestMapping("/update/status")
    @RequiresPermissions("mall:brand:update")
    public AjaxResult updateStatus(@RequestBody BrandEntity brand) {
        brandService.updateById(brand);
        return AjaxResult.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("mall:brand:delete")
    public AjaxResult delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));
        return AjaxResult.success();
    }
}
